package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import model.Model;
import controler.Controler;

/**
 * 
 * @author Schweingruber Roger
 * @date 08.02.2012
 * @version 1.0
 *
 */
@SuppressWarnings("serial")
public class View extends JPanel implements Observer {

	private Model model;
	private Controler controler;

	private JLabel lblThreadInfo1, lblThreadInfo2, lblThreadInfo3;
	private JLabel lblGreen1, lblGreen2, lblGreen3;

	private JButton btnabort, btnclose;

	private JProgressBar pgbThreadProgressPanel1, pgbThreadProgressPanel2,
			pgbThreadProgressPanel3;

	private JPanel pnlThreadInfoPanel1, pnlThreadInfoPanel2,
			pnlThreadInfoPanel3, pnlButtons;

	private int delay1;
	private int delay2;
	private int delay3;

	private int zaehler1;
	private int zaehler2;
	private int zaehler3;

	/**
	 * Konstruktor
	 * 
	 * @param anzModel
	 */
	public View(Model m, final String[] args) {

		this.model = m;
		model.addObserver(this);

		this.setLayout(new GridLayout(7, 1, 3, 3));
		this.setPreferredSize(new Dimension(600, 500));

		initWidgets();

		addWidgets();
		
		controler = new Controler(model, args);
		
		btnclose.addActionListener(controler);
		btnabort.addActionListener(controler);

	}

	/**
	 * Initialisieren der Widgets
	 */
	private void initWidgets() {
		lblThreadInfo1 = new JLabel();
		lblThreadInfo1.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));

		lblThreadInfo2 = new JLabel();
		lblThreadInfo2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));

		lblThreadInfo3 = new JLabel();
		lblThreadInfo3.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));

		pnlThreadInfoPanel1 = new JPanel(new BorderLayout(1, 1));
		pnlThreadInfoPanel2 = new JPanel(new BorderLayout(1, 1));
		pnlThreadInfoPanel3 = new JPanel(new BorderLayout(1, 1));
		pnlButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));

		pgbThreadProgressPanel1 = new JProgressBar();
		pgbThreadProgressPanel2 = new JProgressBar();
		pgbThreadProgressPanel3 = new JProgressBar();

		lblGreen1 = new JLabel(new ImageIcon(getClass().getClassLoader()
				.getResource("img/green.gif")));
		lblGreen2 = new JLabel(new ImageIcon(getClass().getClassLoader()
				.getResource("img/green.gif")));
		lblGreen3 = new JLabel(new ImageIcon(getClass().getClassLoader()
				.getResource("img/green.gif")));

		btnabort = new JButton("Threads abbrechen");
		btnabort.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));

		btnclose = new JButton("Beenden");
		btnclose.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		
	}

	/**
	 * Hinzufuegen der Wigdets
	 */
	private void addWidgets() {
		pnlThreadInfoPanel1.add(BorderLayout.CENTER, lblThreadInfo1);
		pnlThreadInfoPanel1.add(BorderLayout.EAST, lblGreen1);

		pnlThreadInfoPanel2.add(BorderLayout.CENTER, lblThreadInfo2);
		pnlThreadInfoPanel2.add(BorderLayout.EAST, lblGreen2);

		pnlThreadInfoPanel3.add(BorderLayout.CENTER, lblThreadInfo3);
		pnlThreadInfoPanel3.add(BorderLayout.EAST, lblGreen3);

		pnlButtons.add(btnabort);
		pnlButtons.add(btnclose);

		add(pnlThreadInfoPanel1);
		add(pgbThreadProgressPanel1);

		add(pnlThreadInfoPanel2);
		add(pgbThreadProgressPanel2);

		add(pnlThreadInfoPanel3);
		add(pgbThreadProgressPanel3);

		add(pnlButtons);
	}

	/**
	 * Empfangen der aenderungen
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg1.equals("Balken 1")) {
			pgbThreadProgressPanel1.setValue(model.getWertBalken1());
		} else if (arg1.equals("Balken 2")) {
			pgbThreadProgressPanel2.setValue(model.getWertBalken2());
		} else if (arg1.equals("Balken 3")) {
			pgbThreadProgressPanel3.setValue(model.getWertBalken3());
		} else if (arg1.equals("delay")) {
			delay1 = model.getDelay1();
			delay2 = model.getDelay2();
			delay3 = model.getDelay3();

			zaehler1 = model.getZaehler1();
			zaehler2 = model.getZaehler2();
			zaehler3 = model.getZaehler3();

			lblThreadInfo1.setText("Thread 1 mit " + delay1
					+ "ms Verzoegerung hat zum " + zaehler1
					+ ". Mal 100 erreicht");
			lblThreadInfo2.setText("Thread 2 mit " + delay2
					+ "ms Verzoegerung hat zum " + zaehler2
					+ ". Mal 100 erreicht");
			lblThreadInfo3.setText("Thread 3 mit " + delay3
					+ "ms Verzoegerung hat zum " + zaehler3
					+ ". Mal 100 erreicht");

			System.out.println("Delay empfangen");
		} else if (arg1.equals("aktiv")) {
			lblGreen1.setVisible(model.getAktiv1());
			lblGreen2.setVisible(model.getAktiv2());
			lblGreen3.setVisible(model.getAktiv3());
		}

	}

}
